package com.hl.mapper;

import com.hl.pojo.Question;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface QuestionMapper {

    @Insert("insert into question (question_title, question_content,system_user_id,time) values (#{questionTitle}, #{questionContent},#{systemUserId},#{time})")
    public Integer add(Question question);

    @Update("update question set question_title=#{questionTitle}, question_content=#{questionContent}, del=#{del} where id=#{id}")
    public Integer update(Question question);

    @Update("update  question  set del = 1 where id = #{id}")
    public Integer delete(Integer id);

    @Select("select q.*,q.id id1 from question q where id = #{id} and del != 1")
    @Results({
            @Result(property = "username",column = "system_user_id" ,
                    one = @One(select = "a1")),
            @Result(property = "sum",column = "id1" ,
                    one = @One(select = "a2"))
    })
    public Question findById(Integer id);

    @Select("select * from question where del = 0 and kecheng_id is null and question_title like concat('%',#{title},'%')")
    @Results({
            @Result(property = "username",column = "system_user_id" ,
                    one = @One(select = "a1"))
    })
    public List<Question> sosuo(String title);

    @Select("SELECT q.*,q.id id1 FROM question q WHERE del != 1 and kecheng_id is null order by time desc")
    @Results({
            @Result(property = "username",column = "system_user_id" ,
                    one = @One(select = "a1")),
            @Result(property = "sum",column = "id1" ,
                    one = @One(select = "a2"))
    })
    public List<Question> findAll();

    @Select("select username from system_user where id = #{id}")
    public String a1(Integer id);

    @Select("select count(*) from reply where question_id = #{id}")
    public Integer a2(Integer id);


    @Select("SELECT q.*,q.id id1,q.id id2 FROM question q WHERE del != 1 and kecheng_id is null")
    @Results({
            @Result(property = "username",column = "system_user_id" ,
                    one = @One(select = "a1")),
            @Result(property = "sum",column = "id1" ,
                    one = @One(select = "a2")),
            @Result(property = "replyDate",column = "id2" ,
                    one = @One(select = "a3"))
    })
    public List<Question> findAll1();

    @Select("select * from question where del = 0 ")
    public List<Question> findAll4();

    @Select("select time from reply where del = 0 and question_id = #{id} order by time desc limit 1")
    public String a3(Integer id);


    @Select("select q.*,q.id id1,q.id id2 from question q where del=0 and kecheng_id = #{id}")
    @Results({
            @Result(property = "username",column = "system_user_id" ,
                    one = @One(select = "a1")),
            @Result(property = "sum",column = "id1" ,
                    one = @One(select = "a2")),
            @Result(property = "replyDate",column = "id2" ,
                    one = @One(select = "a3"))
    })
    public List<Question> findkecheng(Integer id);




}

